package com.example.cpl.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button but_login;
    private EditText ID,Password;
    public static String filename = "MySharedString_Main";
    static int who=0;
    public void  write_SP_Str(String s,String s1) {         //写入数据库
        final SharedPreferences someData = getSharedPreferences(filename, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putString(""+s+"",""+s1+"");
        editor.commit();// 提交修改
    }
    public String read_SP_Str(String s,String s1) {     //读取数据库
        final SharedPreferences someData = getSharedPreferences(filename, 0);
        return  someData.getString(""+s+"",""+s1+"");
    }
    public void clear_SP(String file) {//           清除数据库
        final SharedPreferences someData = getSharedPreferences(file, 0);
        SharedPreferences.Editor editor = someData.edit();
        editor.clear();
        editor.commit();
    }
    private void initWidght(){          //初始化
        but_login=(Button)findViewById(R.id.but_login);
        ID=(EditText)findViewById(R.id.ID);
        Password=(EditText)findViewById(R.id.Password);
    }
    private void setIDPassword()
    {

        ID.setText(         read_SP_Str("ID","")        );
        Password.setText(       read_SP_Str("Password","")      );
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWidght();
        setIDPassword();

        but_login.setOnClickListener(new View.OnClickListener() {       //设置按钮监听事件
            @Override
            public void onClick(View view) {

                if(!ID.getText().toString().equals("")&&!Password.getText().toString().equals("")){ //判断ID和password是否为空
                    final String id=ID.getText().toString();        //得到ID 强制转换string
                    final String ps = Password.getText().toString();
                    if((ps.equals("12345"))&&(id.equals("chen")))
                    {

                        who=1;
                        write_SP_Str("ID", ID.getText().toString());
                        write_SP_Str("Password", Password.getText().toString());
                        Intent intent = new Intent(MainActivity.this,View1.class);// 跳转到指定界面
                        startActivity(intent);                                                     //
                    }
                    else if((ps.equals("12345"))&&(id.equals("liu")))
                    {

                        who=2;
                        write_SP_Str("ID", ID.getText().toString());
                        write_SP_Str("Password", Password.getText().toString());
                        Intent intent = new Intent(MainActivity.this,View1.class);
                        startActivity(intent);
                    }
                    else if((ps.equals("12345"))&&(id.equals("zhang")))
                    {

                        who=3;
                        write_SP_Str("ID", ID.getText().toString());
                        write_SP_Str("Password", Password.getText().toString());
                        Intent intent = new Intent(MainActivity.this,View1.class);
                        startActivity(intent);
                    }
                    else
                    {
                        clear_SP(filename);
                        Toast.makeText(getApplicationContext(), "账号/密码错误", Toast.LENGTH_SHORT)
                                .show();//阴影提示框
                    }
                }
                else
                {
                    clear_SP(filename);


                    Toast.makeText(getApplicationContext(), "请输入账号密码", Toast.LENGTH_SHORT)
                            .show();        //阴影提示框
                }
            }


        });


    }
}
